module Main where

import Data.Maybe
import Control.Monad
import System.IO
import System.Exit

import DataTypes
import StationManagement
import TrainManagement
import GenericFunctions
import Tools

-- *********************************
-- *  Stations managment interface *
-- *********************************

-- Connections managment submenu.
connectionsSearchMenu = 
    do  stations <- StationManagement.getStations
        if ((length stations) < 2) then
            putStrLn "Minimum two stations required to exist"
        else do
            mstart <- StationManagement.promptStation' stations
            if (isJust mstart) then do
                let start = fromJust mstart
                mmeta <- StationManagement.promptStation' $ filter (\s -> s /= start) stations
                if (isJust mmeta) then do                    
                    let meta = fromJust mmeta
                    date <- GenericFunctions.promptDate "When would you like to travel?"
                    changes <- prompt "How many train changes would you like to make (maximum)?"
                    trains <- TrainManagement.getTrains
                    let availableTrains = filter (\(Train _ since till _ _) -> since <= date && date <= till) trains
                    if (not (null availableTrains)) then do 
                        nodes <- Tools.createNodes availableTrains []
                        let paths = Tools.bfs nodes [DataTypes.id start] [] (DataTypes.id meta)
                        let validPaths = Tools.checkTrainChanges nodes paths (changes + 1)
                        Tools.printValidRoutes validPaths nodes
                        pressEnter
                    else
                        putStrLn "No connections found in that day!"
                else putStrLn "Canceled"
            else putStrLn "Canceled"

-- Stations managment submenu.
stationsSubmenu =   showMenu "STATIONS MANAGEMENT MENU"
                             [("Add new railway station", StationManagement.addStation),
                              ("List the existing railway stations", StationManagement.printStations),
                              ("Edit an existing railway station", StationManagement.editStation),
                              ("Delete an existing railway station", StationManagement.removeStation),
                              ("Cancel", return ())]
                             
-- Trains managment submenu.
trainsSubmenu = showMenu "TRAINS MANAGEMENT MENU"
                         [("Add a new train/Add a new train course with the same route", TrainManagement.addTrain),
                          ("Print existing trains", TrainManagement.printTrains),
                          ("Edit an existing train", TrainManagement.editTrain),
                          ("Delete an existing train", TrainManagement.removeTrain),
                          ("Cancel", return ())]

-- Application's main function
main =  forever $ showMenu "MAIN MENU"
                           [("Railway stations management", stationsSubmenu),
                            ("Trains managment", trainsSubmenu),
                            ("Connections search", connectionsSearchMenu),
                            ("Schedulte generation", Tools.generateSchedule),
                            ("Exit", exitSuccess)]                     

